﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Hotel
{
    public partial class UCLogin : UserControl
    {
        public UCLogin()
        {
            InitializeComponent();
        }

        CNDatabase dt = new CNDatabase();
        // Button Enter
        private void BtEnter_Click(object sender, EventArgs e)
        {
            try
            {
                if (TbID.Text == "" && TbPW.Text == "")
                {
                    MessageBox.Show("Login failure! Please try again.");
                    TbPW.Text = "";
                    TbID.Text = "";
                    TbID.Focus();
                }
                else
                {
                    //dt.knoi();
                    //string query = "SELECT * FROM Account WHERE ([IDAccount] = '" + TbID.Text + "' AND [Password]='" + TbPW.Text + "')";
                    //SqlCommand cm = new SqlCommand(query, dt.cn);
                    //object a = cm.ExecuteScalar();
                    string query = "SELECT * FROM Account WHERE ([IDAccount] = '" + TbID.Text + "' AND [Password]='" + TbPW.Text + "')";
                    object a = dt.xlExecuteScalar(query);                   
                    if (a == null)
                    {
                        MessageBox.Show("Login failure! Please try again.");
                        TbPW.Text = "";
                        TbID.Text = "";
                        TbID.Focus();
                    }
                    else
                    {
                        this.Hide();
                        Profiles pf = new Profiles();
                        Profiles.UserName = TbID.Text;
                        pf.Show();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Login failure! Please try again.");
            }
        }
       
        //Button Cancel
        private void BtCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        //Press Enter to Login
        private void TbPW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    if (TbID.Text == "" && TbPW.Text == "")
                    {
                        MessageBox.Show("Login failure! Please try again.");
                        TbPW.Text = "";
                        TbID.Text = "";
                        TbID.Focus();
                    }
                    else
                    {
                        string query = "SELECT * FROM Account WHERE ([IDAccount] = '" + TbID.Text + "' AND [Password]='" + TbPW.Text + "')";
                        object a = dt.xlExecuteScalar(query); 
                        if (a == null)
                        {
                            MessageBox.Show("Login failure! Please try again.");
                            TbPW.Text = "";
                            TbID.Text = "";
                            TbID.Focus();
                        }
                        else
                        {
                            this.Hide();
                            Profiles pf = new Profiles();
                            Profiles.UserName = TbID.Text;
                            pf.Show();

                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Login failure! Please try again.");
                }
            }
            
        }       
        
    }
}
